//
//  Directory.m
//  HandPay
//
//  Created by xy on 10-3-24.
//  Copyright 2010 HANDPAY. All rights reserved.
//

#import "Directory.h"
#import <sys/mount.h>

const static NSString * SaveRoot=nil;

@implementation Directory

+(BOOL)createDirectory:(NSString *)path{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	return [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
	//return [fileManager createDirectoryAtPath:path attributes:nil];
}

	
+(BOOL)exists:(NSString *)path{
	BOOL isDir;
	NSFileManager *fileManager = [NSFileManager defaultManager];
	return [fileManager fileExistsAtPath:path isDirectory:&isDir];
}

+(long)freeSpaceSize:(NSString *)path{
	struct statfs buf;
	long long  totalspace2=0;
	if (statfs("/private/var", &buf)>=0) {
		totalspace2=(long long)buf.f_bsize * buf.f_bfree;
	}
	totalspace2/=1024;
	return (long)totalspace2;
}
+(NSString *)getHandPayRoot{
	return @"handPay";
}
+(const NSString *)getSaveRoot{
	if (SaveRoot==nil) {
		NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		SaveRoot=[paths objectAtIndex:0];
		[SaveRoot retain];
	}
	return SaveRoot;
}
+(NSString *)getCardRoot{
	return nil;
}
+(NSArray *)getFilesInPath:(NSString *)path withFilter:(NSString *)filter{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	//return [fileManager directoryContentsAtPath:path];
	return [fileManager contentsOfDirectoryAtPath:path error:nil];
}
+(long)totalSpaceSize{
	struct statfs buf;
	long long  totalspace2=0;
	if (statfs("/private/var", &buf)>=0) {
		totalspace2=(long long)buf.f_bsize * buf.f_blocks;
	}
	totalspace2/=1024;
	return (long)totalspace2;
}
+(NSArray *)getChidrenInPath:(NSString *)path{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	//return [fileManager directoryContentsAtPath:path];
	return [fileManager contentsOfDirectoryAtPath:path error:nil];
}


+(NSString *)getFilePath{
	NSString * result=[Directory getSaveRoot];
	result=[result stringByAppendingPathComponent:@"handPay/file"];
	return result;
}
+(NSString *)getCachePath{
	NSString * result=[Directory getSaveRoot];
	result=[result stringByAppendingPathComponent:@"handPay/cache"];
	return result;
}
+(NSString *)gettempPath{
	NSString * result=[Directory getSaveRoot];
	result=[result stringByAppendingPathComponent:@"handPay/temp"];
	return result;
}

@end
